﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="IsTransferNeeded.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System;
    using System.Data.SqlClient;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper.Database;

    /// <summary>
    /// Task for checking is a table has been updated since it was last transferred
    /// </summary>
    public class IsTransferNeeded : EmbeddedBoolean<MSSql10, SqlParameter>
    {
        /// <summary>
        /// Name of the table to check
        /// </summary>
        private readonly string tableName;

        /// <summary>
        /// The date which will indicate we need to do a transfer
        /// </summary>
        private readonly DateTime sinceDate;

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="IsTransferNeeded"/> class. 
        /// </summary>
        /// <param name="connectionString">
        /// Database connection string
        /// </param>
        /// <param name="tableName">
        /// Name of the table to check
        /// </param>
        /// <param name="sinceDate">
        /// Date the table was last transferred
        /// </param>
        /// <param name="assumeSqlSafe">
        /// Whether to skip the SQL injection safety check.  Use only if you know the code you are passing in is safe and contains a quoted string that is preventing it passing the saftey check
        /// </param>
        public IsTransferNeeded(string connectionString, string tableName, DateTime sinceDate, bool assumeSqlSafe)
            : base(
                "Checking if table transfer needed", 
                connectionString, 
                Assembly.GetExecutingAssembly(), 
                "Dhgms.TplHelper.Resource.Sql", 
                "IsTransferNeeded.sql",
                assumeSqlSafe)
        {
            this.tableName = tableName;
            this.sinceDate = sinceDate;
        }

        #endregion

        /// <summary>
        /// Gets a list of parameters for the SQL command
        /// </summary>
        /// <returns>
        /// list of parameters for the SQL command
        /// </returns>
        protected override SqlParameter[] GetParameters()
        {
            return new[] { new SqlParameter("@sinceDate", this.sinceDate), new SqlParameter("@tableName", this.tableName) };
        }
    }
}